package com.changfeng.service.impl;

import com.changfeng.auth.dto.UserAuth;
import com.changfeng.bean.ReatailersAddress;
import com.changfeng.bean.dto.vo.AddressListVO;
import com.changfeng.bean.dto.vo.AddressSaveVO;
import com.changfeng.common.CurrentUser;
import com.changfeng.dao.ReatailersAddressMapper;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private ReatailersAddressMapper reatailersAddressMapper ;

    @Override
    public void SaveAddress(AddressSaveVO addressSaveVO) {
        ReatailersAddress reatailersAddress = null;
        UserAuth userAuth = CurrentUser.getCurrentUser();
        if (addressSaveVO.getAddressId() == null ) {
            reatailersAddress = new ReatailersAddress();
            reatailersAddress.setUserId(userAuth.getUserId());
            reatailersAddress.setAddressDescribe(addressSaveVO.getAddressDescribe());
            reatailersAddress.setCountryId(addressSaveVO.getCountryId());
            reatailersAddress.setProvinceId(addressSaveVO.getProvinceId());
            reatailersAddress.setCityId(addressSaveVO.getCityId());
            reatailersAddress.setAddressName(addressSaveVO.getAddressName());
            reatailersAddress.setAddressPhone(addressSaveVO.getAddressPhone());
            reatailersAddress.setGmtCreate(new Date());
            reatailersAddress.setGmtModified(new Date());
        } else {
            reatailersAddress = reatailersAddressMapper.selectByPrimaryKey(addressSaveVO.getAddressId());
            if (reatailersAddress == null) {
                throw new ChangFengException(ChangFengExceptionEnum.ERROR);
            }
            reatailersAddress.setUserId(userAuth.getUserId());
            reatailersAddress.setAddressDescribe(addressSaveVO.getAddressDescribe());
            reatailersAddress.setCountryId(addressSaveVO.getCountryId());
            reatailersAddress.setProvinceId(addressSaveVO.getProvinceId());
            reatailersAddress.setCityId(addressSaveVO.getCityId());
            reatailersAddress.setAddressName(addressSaveVO.getAddressName());
            reatailersAddress.setAddressPhone(addressSaveVO.getAddressPhone());
            reatailersAddress.setGmtModified(new Date());
        }
        if (reatailersAddress == null) {
            throw new ChangFengException(ChangFengExceptionEnum.ERROR);
        }
        if (reatailersAddress.getAddressId() == null) {
            reatailersAddressMapper.insert(reatailersAddress);
        } else {
            reatailersAddressMapper.updateByPrimaryKey(reatailersAddress);
        }

    }

    @Override
    public List<AddressListVO> getAddressList(Integer userId) {
        return reatailersAddressMapper.selectByUserId(userId);
    }

    @Override
    public AddressSaveVO getAddress(Integer addressId) {
        ReatailersAddress reatailersAddress = reatailersAddressMapper.selectByPrimaryKey(addressId);
        if (reatailersAddress == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        AddressSaveVO addressVO = new AddressSaveVO();
        addressVO.setCityId(reatailersAddress.getCityId());
        addressVO.setAddressDescribe(reatailersAddress.getAddressDescribe());
        addressVO.setAddressId(reatailersAddress.getAddressId());
        addressVO.setAddressName(reatailersAddress.getAddressName());
        addressVO.setAddressPhone(reatailersAddress.getAddressPhone());
        addressVO.setCountryId(reatailersAddress.getCountryId());
        addressVO.setProvinceId(reatailersAddress.getProvinceId());
        return addressVO;
    }

    @Override
    public void deleteAddress(Integer addressId) {
        reatailersAddressMapper.deleteByPrimaryKey(addressId);
    }
}
